//
//  DepartOneViewController.m
//  zhixinApp
//
//  Created by zhiguanzhl on 2017/11/16.
//  Copyright © 2017年 zhiguanzhl. All rights reserved.
//

#import "DepartOneViewController.h"
#import "ArrowsCell.h"
#import "SingleChatViewController.h"


@interface DepartOneViewController ()<UITableViewDelegate ,UITableViewDataSource>
@property (nonatomic ,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *nameArr;
@property (nonatomic,strong)NSMutableArray *idArr;
@property (nonatomic,strong)NSMutableArray *countArr;
@property (nonatomic,strong)NSMutableArray *logiNameArr;
@property (nonatomic,strong)NSNumber *numberPara;
@end

@implementation DepartOneViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationItem.title = @"组织架构";
//    self.numberPara = @0;
    //设置回调（一旦进入刷新状态就会调用这个刷新）
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        [self loadData];
//    }];
    [self initSubView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self.tableView.mj_header beginRefreshing];
    [self loadData];
    
}

- (void)initSubView{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT-64) style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.tableFooterView = [[UIView alloc] init];
    self.tableView = tableView;
    [self.view addSubview:tableView];
    
}
//获取组织架构
- (void)loadData{
    
    NSMutableDictionary * paramDict = [[NSMutableDictionary alloc] init];
    NSLog(@"%@",self.companyId);
//    [paramDict setValue: [DataCenter shareInstence].companyId forKey:@"companyId"];
    [paramDict setValue: self.companyId forKey:@"companyId"];
    [paramDict setValue:self.numberPara forKey:@"deptId"];
    NSLog(@"%@",paramDict);
    @WeakObj(self);
    [ZLHXHelper postURL:@"organization/dept" paramDict:paramDict completion:^(NSDictionary *responseObject) {
        
        NSDictionary *dic = [responseObject objectForKey:@"obj"];
        NSArray *dataArr = [dic objectForKey:@"depts"];
        NSArray *userArr = [dic objectForKey:@"users"];
        [selfWeak.nameArr removeAllObjects];
        [selfWeak.idArr removeAllObjects];
        [selfWeak.countArr removeAllObjects];
        [selfWeak.logiNameArr removeAllObjects];
        
        for (int i = 0; i<dataArr.count; i++) {
            NSString *name = [dataArr[i] valueForKey:@"name"];
            
            NSInteger iid = [[dataArr[i] valueForKey:@"id"] integerValue];            NSNumber *idNumber = [NSNumber numberWithInteger:iid];
            [selfWeak.nameArr addObject:name];
            [selfWeak.idArr addObject:idNumber];
            NSInteger count = [[dataArr[i] valueForKey:@"total"] integerValue];            NSNumber *countNumber = [NSNumber numberWithInteger:count];
            [selfWeak.countArr addObject:countNumber];
            
        }
        
        for (int j=0; j<userArr.count; j++) {
            NSString *name = [userArr[j] valueForKey:@"name"];
            NSString *logaiName = [userArr[j] valueForKey:@"loginName"];
            [selfWeak.nameArr addObject:name];
            [selfWeak.logiNameArr addObject:logaiName];
            NSInteger iid = [[userArr[j] valueForKey:@"id"] integerValue];           NSNumber *idNumber = [NSNumber numberWithInteger:iid];
            [selfWeak.idArr addObject:idNumber];
            
        }
        
        [selfWeak.tableView reloadData];
        
        [selfWeak.tableView.mj_header endRefreshing];
        
        
    } error:^(NSError *error) {
        
        [selfWeak.tableView.mj_header endRefreshing];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.nameArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  56;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ArrowsCell *arrowCell = [ArrowsCell cellWithTableView:tableView];
     NSLog(@"%@",arrowCell.titleLab);
    arrowCell.titleLab.text = self.nameArr[indexPath.row];
    NSString *str = self.nameArr[indexPath.row];
    if (str.length >2) {
        str = [str substringFromIndex:1];
    }
    
    if (self.countArr.count != 0) {
        //加这种情况的原因是避免又有人又有组
        if (indexPath.row <= self.countArr.count-1) {
            arrowCell.countLable.text = [Tool numberToStrWithNum:self.countArr[indexPath.row]];
            arrowCell.btnConstrain.constant = 0;
            arrowCell.lableConstraints.constant = 0;
            
            
        }else{
           
            [arrowCell.headImage setTitle:str forState:UIControlStateNormal];
            arrowCell.backView.hidden = YES;
        }
        
        
    }else{
        //只有人
         [arrowCell.headImage setTitle:str forState:UIControlStateNormal];
        arrowCell.backView.hidden = YES;
    }
    
    
    return arrowCell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (!self.countArr.count) {
        
      
        SingleChatViewController *chatController = [[SingleChatViewController alloc] initWithConversationChatter:self.logiNameArr[indexPath.row] conversationType:EMConversationTypeChat];
        chatController.title = self.nameArr[indexPath.row];
        [self.navigationController pushViewController:chatController animated:YES];
        
        return;
    }else{
        
        if (self.countArr.count-1 < indexPath.row) {
            SingleChatViewController *chatController = [[SingleChatViewController alloc] initWithConversationChatter:self.logiNameArr[indexPath.row-self.countArr.count] conversationType:EMConversationTypeChat];
            chatController.title = self.nameArr[indexPath.row];
            [self.navigationController pushViewController:chatController animated:YES];
            return;
        }else {
            
            if ([self.countArr[indexPath.row] integerValue] == 0) {
                iToast *toast = [[iToast alloc] initWithText:@"下级没有人了，不可点击"];
                [toast show];
                return;
            }
            
        }
    }
    
     DepartOneViewController *departVC = [[DepartOneViewController alloc] init];
    departVC.numberPara = self.idArr[indexPath.row];
    departVC.companyId = self.companyId;
    [self.navigationController pushViewController:departVC animated:YES];

   
}


- (NSMutableArray *)nameArr{
    if (!_nameArr) {
        _nameArr = [[NSMutableArray alloc] init];
    }
    
    return _nameArr;
}
- (NSMutableArray *)idArr{
    if (!_idArr) {
        _idArr = [[NSMutableArray alloc] init];
    }
    
    return _idArr;
}
- (NSMutableArray *)countArr{
    if (!_countArr) {
        _countArr = [[NSMutableArray alloc] init];
    }
    
    return _countArr;
}

- (NSMutableArray *)logiNameArr{
    if (!_logiNameArr) {
        _logiNameArr = [[NSMutableArray alloc] init];
    }
    
    return _logiNameArr;
}


@end
